/*
 * Created by JFormDesigner on Fri Sep 10 09:06:38 CST 2021
 */

package com.minelx.woods.pc.form;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.minelx.httpclient.core.request.IResponse;
import com.minelx.woods.pc.service.ReviewService;
import com.minelx.woods.pc.service.TaskService;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;

import static java.nio.charset.StandardCharsets.UTF_8;
import static javax.swing.JOptionPane.showMessageDialog;

/**
 * @author aaa
 */
public class ReviewDetailDialog extends JDialog {

	private final JSONObject reviewAsJSON;

	private final ReviewService reviewService;

	private final TaskService taskService;

	public ReviewDetailDialog(Window owner,
	                          ReviewService reviewService,
	                          TaskService taskService,
	                          JSONObject reviewAsJSON) {
		super(owner);
		this.reviewAsJSON = reviewAsJSON;
		initComponents();

		// preparing
		refreshContent(reviewAsJSON);
		this.reviewService = reviewService;
		this.taskService = taskService;
	}

	private void approveButtonActionPerformed(ActionEvent e) {
		// FIXME 2021/9/9  replace with:  verifying of XXRequestService
		String text = reviewService.approve(reviewAsJSON.getString("id")).contentAsText(UTF_8);
		if (text.isEmpty()) {
			showMessageDialog(this, "请求成功", "成功", JOptionPane.PLAIN_MESSAGE);
		} else {
			showMessageDialog(this, JSON.parseObject(text).get("error"), "错误", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void rejectButtonActionPerformed(ActionEvent e) {
		String text = reviewService.reject(reviewAsJSON.getString("id")).contentAsText(UTF_8);
		if (text.isEmpty()) {
			showMessageDialog(this, "请求成功", "成功", JOptionPane.PLAIN_MESSAGE);
		} else {
			JSONObject responseAsJSON = JSON.parseObject(text);
			showMessageDialog(this, responseAsJSON.get("error"), "错误", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void viewTaskDetailButtonActionPerformed(ActionEvent e) {
		IResponse response = taskService.getTask(reviewAsJSON.getString("taskId"));
		JSONObject taskAsJSON = JSON.parseObject(response.contentAsText(UTF_8));
		new TaskDetailDialog(this, taskService, taskAsJSON).setVisible(true);
	}

	private void refreshContent(JSONObject reviewerAsJSON) {
		reviewNameText.setText(reviewerAsJSON.getString("name"));
		submitterText.setText(reviewerAsJSON.getString("submitterId"));
		List<String> approved = reviewerAsJSON.getJSONArray("approved").toJavaList(String.class);
		approvedReviewersText.setText(approved.toString());
		List<String> reviewers = reviewerAsJSON.getJSONArray("reviewers").toJavaList(String.class);
		reviewersText.setText(reviewers.toString());
		// setting condition text
		boolean isRejected = reviewerAsJSON.getBooleanValue("rejected");
		if (isRejected) {
			reviewConditionText.setText("已驳回.");
		} else { // not rejected
			if (approved.containsAll(reviewers)) {
				reviewConditionText.setText("审核已通过.");
			} else {
				reviewConditionText.setText("等待剩余审核人员审核.");
			}
		}
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		label1 = new JLabel();
		panel1 = new JPanel();
		label8 = new JLabel();
		reviewNameText = new JTextField();
		label9 = new JLabel();
		submitterText = new JTextField();
		label4 = new JLabel();
		reviewConditionText = new JTextField();
		label5 = new JLabel();
		reviewersText = new JTextField();
		label6 = new JLabel();
		approvedReviewersText = new JTextField();
		label7 = new JLabel();
		viewTaskDetailButton = new JButton();
		panel4 = new JPanel();
		approveButton = new JButton();
		rejectButton = new JButton();

		//======== this ========
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		var contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== dialogPane ========
		{
			dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
			dialogPane.setLayout(new BorderLayout());

			//======== contentPanel ========
			{
				contentPanel.setLayout(new BorderLayout());

				//---- label1 ----
				label1.setText("\u5ba1\u6838\u8be6\u60c5");
				label1.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 35));
				label1.setHorizontalAlignment(SwingConstants.CENTER);
				contentPanel.add(label1, BorderLayout.NORTH);

				//======== panel1 ========
				{
					panel1.setLayout(new GridLayoutManager(7, 2, new Insets(0, 0, 0, 0), -1, -1));

					//---- label8 ----
					label8.setText("\u5ba1\u6838\u540d\u79f0: ");
					label8.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
					panel1.add(label8, new GridConstraints(0, 0, 1, 1,
						GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						null, null, null));

					//---- reviewNameText ----
					reviewNameText.setEditable(false);
					reviewNameText.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 26));
					reviewNameText.setColumns(12);
					panel1.add(reviewNameText, new GridConstraints(0, 1, 1, 1,
						GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						null, null, null));

					//---- label9 ----
					label9.setText("\u63d0\u4ea4\u4eba: ");
					label9.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
					panel1.add(label9, new GridConstraints(1, 0, 1, 1,
						GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						null, null, null));

					//---- submitterText ----
					submitterText.setEditable(false);
					submitterText.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 26));
					submitterText.setColumns(12);
					panel1.add(submitterText, new GridConstraints(1, 1, 1, 1,
						GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						null, null, null));

					//---- label4 ----
					label4.setText("\u5ba1\u6838\u72b6\u6001: ");
					label4.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
					panel1.add(label4, new GridConstraints(2, 0, 1, 1,
						GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						null, null, null));

					//---- reviewConditionText ----
					reviewConditionText.setEditable(false);
					reviewConditionText.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 26));
					reviewConditionText.setColumns(12);
					panel1.add(reviewConditionText, new GridConstraints(2, 1, 1, 1,
						GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						null, null, null));

					//---- label5 ----
					label5.setText("\u5ba1\u6838\u4eba\u5458: ");
					label5.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
					panel1.add(label5, new GridConstraints(3, 0, 1, 1,
						GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						null, null, null));

					//---- reviewersText ----
					reviewersText.setEditable(false);
					reviewersText.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 26));
					reviewersText.setColumns(12);
					panel1.add(reviewersText, new GridConstraints(3, 1, 1, 1,
						GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						null, null, null));

					//---- label6 ----
					label6.setText("\u5df2\u5ba1\u6838\u4eba\u5458: ");
					label6.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
					panel1.add(label6, new GridConstraints(4, 0, 1, 1,
						GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						null, null, null));

					//---- approvedReviewersText ----
					approvedReviewersText.setEditable(false);
					approvedReviewersText.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 26));
					approvedReviewersText.setColumns(12);
					panel1.add(approvedReviewersText, new GridConstraints(4, 1, 1, 1,
						GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						null, null, null));

					//---- label7 ----
					label7.setText("\u4efb\u52a1\u8be6\u60c5: ");
					label7.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
					panel1.add(label7, new GridConstraints(5, 0, 1, 1,
						GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						null, null, null));

					//---- viewTaskDetailButton ----
					viewTaskDetailButton.setText("\u67e5\u770b\u4efb\u52a1\u8be6\u60c5");
					viewTaskDetailButton.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 24));
					viewTaskDetailButton.addActionListener(e -> viewTaskDetailButtonActionPerformed(e));
					panel1.add(viewTaskDetailButton, new GridConstraints(5, 1, 1, 1,
						GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
						null, null, null));
				}
				contentPanel.add(panel1, BorderLayout.CENTER);
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);

			//======== panel4 ========
			{
				panel4.setLayout(new FlowLayout(FlowLayout.TRAILING));

				//---- approveButton ----
				approveButton.setText("\u901a\u8fc7");
				approveButton.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
				approveButton.addActionListener(e -> approveButtonActionPerformed(e));
				panel4.add(approveButton);

				//---- rejectButton ----
				rejectButton.setText("\u9a73\u56de");
				rejectButton.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 22));
				rejectButton.addActionListener(e -> rejectButtonActionPerformed(e));
				panel4.add(rejectButton);
			}
			dialogPane.add(panel4, BorderLayout.SOUTH);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		setSize(510, 655);
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JLabel label1;
	private JPanel panel1;
	private JLabel label8;
	private JTextField reviewNameText;
	private JLabel label9;
	private JTextField submitterText;
	private JLabel label4;
	private JTextField reviewConditionText;
	private JLabel label5;
	private JTextField reviewersText;
	private JLabel label6;
	private JTextField approvedReviewersText;
	private JLabel label7;
	private JButton viewTaskDetailButton;
	private JPanel panel4;
	private JButton approveButton;
	private JButton rejectButton;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
